<template>
  <a-card :bordered="false" style="margin-bottom: 10px">
    <BaseInfo :id="personId" />
    <a-button type="primary" class="btn" @click="onAdd()">添加计划</a-button>
    <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="false" :loading="loading" :columns="columns"
      :dataSource="pageData" @change="onTableChange" style="margin-top:20px;">
      <template slot="planTime" slot-scope="text, record">
        <span>{{ record.planStartTime }} ~ {{ record.planEndTime }} </span>
      </template>
      <template slot="typeIntervention" slot-scope="text">
        <span>{{ setTittle(text) }}</span>
      </template>
      <!--操作-->
      <template slot="action" slot-scope="text, record">
        <a-button @click="onDetail(record)" type="link" style="padding: 0">查看</a-button>
      </template>
    </z-table>
    <DetailModal ref="detailModalId" />
    <AddModal ref="addModalId" @ok=onQuery />
  </a-card>
</template>

<script>
import BaseInfo from '@/views/sjsk/modules/BaseInfo'
import AddModal from './modules/AddModal'
import DetailModal from './modules/DetailModal'
import * as helpApi from '@/api/sjsk/help/helpPlan.js'
export default {
  components: {
    BaseInfo, AddModal, DetailModal
  },

  data() {
    return {
      loading: false,
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '计划类型',
          dataIndex: 'planType',
          align: 'center',
        },
        {
          title: '计划内容',
          dataIndex: 'planContent',
          align: 'center',
        },
        {
          title: '计划时间',
          dataIndex: 'planTime',
          align: 'center',
          scopedSlots: { customRender: 'planTime' },
        },
        {
          title: '介入类型',
          dataIndex: 'typeIntervention',
          align: 'center',
          scopedSlots: { customRender: 'typeIntervention' },
        },
        {
          title: '任务量',
          dataIndex: 'taskVolume',
          align: 'center',
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center',
        },
        {
          title: '完成量',
          dataIndex: 'completedQuantity',
          align: 'center',
        },
        {
          title: '操作',
          width: 80,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      pageData: {},
      personId: this.$route.query.personId
    }
  },
  created() {

  },
  async mounted() {
    this.onQuery()
  },

  methods: {
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      let queryObj = {
        id: this.$route.query.id,
        type: this.$route.query.personType
      }
      let params = Object.assign(this.pageFilter, queryObj)
      helpApi.viewSchedule(params).then((res) => {
        if (res.success) {
          this.pageData = res.result ? res.result : {}
          this.loading = false
        } else {
          this.pageData = {}
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.pageData = {}
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    setTittle(text) {
      return this.$filterDictTextByCache('typeIntervention', text)  //现在字典中对应的文字，是全局函数
    },
    onDetail(record) {
      this.$refs.detailModalId.show(record)
    },
    onAdd() {
      this.$refs.addModalId.show()
    },
  },
}
</script>
<style lang="less" scoped>
.btn {
  position: absolute;
  right: 24px;
  top: 50px;
  z-index: 10;
}
</style>
